import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';

class BaseCopiedText extends StatelessWidget {

  final String copiedText;

  final TextStyle copiedTextStyle;

  final String title;

  final TextStyle titleTextStyle;

  /// 是否需要复制图标
  final bool needCopyIcon;
  final double copyIconSize;

  /// 前缀
  final String prefix;
  final TextStyle prefixTextStyle;

  /// 后缀
  final String suffix;
  final TextStyle suffixTextStyle;

  // 子容器
  final Widget child;

  const BaseCopiedText({
    Key key,
    @required this.copiedText,
    this.needCopyIcon = true,
    this.copiedTextStyle,
    this.title = '',
    this.titleTextStyle,
    this.prefix = '',
    this.prefixTextStyle,
    this.suffix = '',
    this.suffixTextStyle,
    this.child,
    this.copyIconSize = 16.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (title.isEmpty && copiedText.isEmpty) return SizedBox();
    if (title.isNotEmpty && copiedText.isEmpty) return Text(title, style: titleTextStyle);
    return InkWell(
      onTap: () => BaseClipboardTool.setDataToast(copiedText),
      child: this.child == null ? this._buildCopiedContainer() : this._buildCopiedChild(),
    );
  }

  Widget _buildCopiedChild() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        this.child,
        if (needCopyIcon) SizedBox(width: 4),
        if (needCopyIcon) Icon(BaseIconData.x3Copy, size: 16),
      ],
    );
  }

  Widget _buildCopiedContainer() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        if (title.isNotEmpty) Text(title, style: titleTextStyle),
        if (title.isNotEmpty) SizedBox(width: 4.0),
        Flexible(child: buildCopiedText()),
        if (needCopyIcon) SizedBox(width: 4),
        if (needCopyIcon) Icon(BaseIconData.x3Copy, size: 16),
      ],
    );
  }

  Widget buildCopiedText() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        if (prefix.isNotEmpty) Text(prefix, style: prefixTextStyle),
        Flexible(child: Text(copiedText, style: copiedTextStyle, maxLines: 1, overflow: TextOverflow.ellipsis)),
        if (suffix.isNotEmpty) Text(suffix, style: suffixTextStyle),
      ],
    );
  }

}
